package com.crack.simpleapiclientsdk.aop;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.crack.simpleapiclientsdk.annotation.RequestMethod;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import com.crack.simpleapiclientsdk.utils.StringUtils;
import java.lang.reflect.Field;
import java.util.HashMap;
import com.crack.simpleapiclientsdk.utils.SignUtils;

/**
 * @version 1.0
 * @Description
 * @CreateTime 2023/3/19 23:07
 */
@Aspect
@Component
public class SignInterceptor {
    public static final String EMPTY_STRING = "";
    public static final String PENDING_URL = "FAKE_URL";

    @Around("@annotation(requestMethod)")
    public Object doInterceptor(ProceedingJoinPoint joinPoint,
                                RequestMethod requestMethod) throws Throwable {
        String method = requestMethod.method().getText();
        //将参数转为String
        Object[] args = joinPoint.getArgs();
        //获取Client类
        Object target = joinPoint.getTarget();
        Class<?> targetClass = target.getClass();
        Field accessKeyField = targetClass.getDeclaredField("accessKey");
        Field secretKeyField = targetClass.getDeclaredField("secretKey");
        accessKeyField.setAccessible(true);
        secretKeyField.setAccessible(true);
        String accessKey = (String) accessKeyField.get(target);
        String secretKey = (String) secretKeyField.get(target);
        //请求参数
        HttpRequest httpRequest = null;
        if("GET".equals(method)){
            //通过StringUtils处理参数
            String arg = EMPTY_STRING;
            if(args[0] instanceof String){
                arg = (String) args[0];
            }else{
                arg = JSONUtil.toJsonStr(args[0]);
            }
            if(StrUtil.isBlankIfStr(arg)){
                arg = "";
            }else{
                arg = StringUtils.formatJsonStrToGetParams(arg);
            }
            args[0] = arg;
            //进行签名
            HashMap<String, String> header = new HashMap<>();
            //Json参数
            header.put("body",arg);
            header.put("accessKey",accessKey);
            //不能在服务器之间传递 secretkey
            header.put("nance", RandomUtil.randomNumbers(4));
            header.put("timestamp",String.valueOf(System.currentTimeMillis()/1000));
            header.put("sign", SignUtils.getSign(header,secretKey));//生成签名
            httpRequest = HttpRequest.get(PENDING_URL)
                    .addHeaders(header);


        }else if("POST".equals(method)){
            //获取方法中的参数类型，统一只有两个参数 1.使用的对象  2.HttpRequest 对象
            Object arg = args[0];
            String json = "";
            //判断
            try {
                if (arg instanceof String){
                    json = StringUtils.formatJsonStrToGetParams((String) arg);
                }else {
                    json = JSONUtil.toJsonStr(arg);
                }
            } catch (Exception e) {
                throw new RuntimeException("系统错误");
            }
            HashMap<String, String> header = new HashMap<>();
            //Json参数
            header.put("body",json);
            header.put("accessKey",accessKey);
            //不能在服务器之间传递 secretkey
            header.put("nance", RandomUtil.randomNumbers(4));
            header.put("timestamp",String.valueOf(System.currentTimeMillis()/1000));
            header.put("sign", SignUtils.getSign(header,secretKey));//生成签名
            httpRequest = HttpRequest.post(PENDING_URL)
                    .addHeaders(header)
                    .body(json);
        }
        args[1] = httpRequest;
        return joinPoint.proceed(args);
    }
}
